package com.accesshollywood.grfxOrdering.panels;

import java.awt.Component;
import java.awt.Dimension;
import java.util.ArrayList;
import java.util.EnumSet;

import javax.swing.BoxLayout;
import javax.swing.JComponent;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.border.EtchedBorder;
import javax.swing.border.TitledBorder;

import com.accesshollywood.components.BasicPopupMenu;
import com.accesshollywood.components.EditableListWithPopup;
import com.accesshollywood.components.EditableListWithPopup.EditableListModel;
import com.accesshollywood.components.HasPopupMenu;
import com.accesshollywood.grfxOrdering.basics.Users;
import com.accesshollywood.indexables.GrfxOrder;
import com.accesshollywood.indexables.User;
import com.accesshollywood.indexables.items.TextIndexed;
import com.accesshollywood.indexables.items.TextSet;
import com.accesshollywood.prefs.Prefs;

public class ItemTextPanel extends JPanel implements HasPopupMenu {
	public GrfxOrder				grfxOrder;
	JComponent						parent;
	// public EditableOneColumnTable textTable;
	public EditableListWithPopup	textList;

	public ItemTextPanel(GrfxOrder grfxOrder, JComponent parent, boolean useApproval) {
		super();
		this.grfxOrder = grfxOrder;
		this.parent = parent;
	}

	public void createPanel(TextSet textSet) {

		setBorder(new TitledBorder(new EtchedBorder(), "TEXT"));

		setAlignmentX(Component.LEFT_ALIGNMENT);
		setAlignmentY(Component.TOP_ALIGNMENT);
		setLayout(new BoxLayout(this, BoxLayout.X_AXIS));
		/*
		 * textTable = new EditableOneColumnTable(this, "text", "text", "idText", "textList", "idText", "idTextList", "", listing.id, Prefs.PixelSize.textList.get(), true);
		 * textTable.setAlignmentX(Component.LEFT_ALIGNMENT);
		 * if (grfxOrder != null) {
		 * textTable.getModel().create(listing, grfxOrder.getAssignedUsers());
		 * }
		 * textTable.setTableHeader(null);
		 * textTable.setFillsViewportHeight(true);
		 * textTable.putClientProperty("terminateEditOnFocusLost", Boolean.TRUE);
		 * textTable.setShowGrid(false);
		 * textTable.setShowHorizontalLines(true);
		 * textTable.setShowVerticalLines(false);
		 */

		// int h = (textTable.getModel().getRowCount() + 1) * textTable.getRowHeight();

		// JScrollPane scrollPane = new JScrollPane(textTable);
		// scrollPane.setPreferredSize(new Dimension(275, h));


		textList = new EditableListWithPopup<TextIndexed>(createModel(textSet, grfxOrder.getAssignedUsers()), Prefs.PixelSize.textList.get(), TextIndexed.class);
		
		int h = (textList.getModel().getSize() + 2) * Prefs.PixelSize.textList.get();
		JScrollPane scrollPane = new JScrollPane(textList);
		scrollPane.setAlignmentX(Component.LEFT_ALIGNMENT);
		scrollPane.setPreferredSize(new Dimension(275, h));

		setupPopup();
		add(scrollPane);
	}

	public void setupPopup() {
		if (Users.currentUser.canEditText(grfxOrder)) {
			textList.popup.popupItems = EnumSet.of(
					BasicPopupMenu.POPUP_ITEMS.ADD,
					BasicPopupMenu.POPUP_ITEMS.EDIT,
					BasicPopupMenu.POPUP_ITEMS.COPY,
					BasicPopupMenu.POPUP_ITEMS.READ_ALOUD,
					BasicPopupMenu.POPUP_ITEMS.SPELL_ALOUD,
					BasicPopupMenu.POPUP_ITEMS.DELETE
					);
		} else {
			textList.popup.popupItems = EnumSet.of(
					BasicPopupMenu.POPUP_ITEMS.COPY,
					BasicPopupMenu.POPUP_ITEMS.READ_ALOUD,
					BasicPopupMenu.POPUP_ITEMS.SPELL_ALOUD
					);
		}
		textList.setupPopup();
		// Trace.info("hi");
	}
	
	public EditableListModel createModel(TextSet textSet, ArrayList<User> permittedUsers){
		/*
		EditableListModel model = new EditableListModel();
		
		for (int i = 0; i < textSet.size(); i++) {
			Object currLine = textSet.get(i);
			model.addElement(currLine);
		}
		
		return model;
		*/
		return textSet.toEditableListModel();
	}
}
